﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Globalization;
using Shop_Phone.Entities;
using System.Data;

namespace Shop_Phone.COMM
{
    public class EndUser
    {

        public DataTable getby_chitiet_sanpham(string _masanpham, ref string errMsg)
        {
            try
            {
                SqlParameter[] prms = new SqlParameter[1];
                prms[0] = new System.Data.SqlClient.SqlParameter("@masanpham", _masanpham);
                DataTable dtTon_VatTu = null;
                dtTon_VatTu = SQLExecute.LoadDataFromDB("dbo.sp_getby_chitiet_sanpham", "tmpData", ref errMsg, CommandType.StoredProcedure, prms, 1);
                return dtTon_VatTu;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                throw ex;
            }
        }

        public int save_hoadon(hoadon vhoadon, ref string errMsg)
        {
            try
            {
                errMsg = "";
                int i = 0;
                int result = 0;
                SqlParameter[] prms = new SqlParameter[10];
                prms[i] = new SqlParameter("@mahoadon", vhoadon.Mahoadon);
                i = i + 1;
                prms[i] = new SqlParameter("@tenkhachhang", vhoadon.Tenkhachhang);
                i = i + 1;
                prms[i] = new SqlParameter("@diachi", vhoadon.Diachi);
                i = i + 1;
                prms[i] = new SqlParameter("@sodienthoai", vhoadon.Sodienthoai);
                i = i + 1;
                prms[i] = new SqlParameter("@email", vhoadon.Email);
                i = i + 1;
                prms[i] = new SqlParameter("@ngaydat", vhoadon.Ngaydat);
                i = i + 1;
                prms[i] = new SqlParameter("@ngaygiao", vhoadon.Ngaygiao);
                i = i + 1;
                prms[i] = new SqlParameter("@trangthai", vhoadon.Trangthai);
                i = i + 1;
                prms[i] = new SqlParameter("@outreturn", SqlDbType.Int, 1);
                prms[i].Direction = ParameterDirection.Output;

                result = SQLExecute.ExcuteSPOutput("dbo.sp_save_hoadon", ref prms, 9, ref errMsg, "@outreturn");

                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return 0;
            }
        }

        public int save_chitiet_hoadon(chitiethoadon vchitiethoadon, ref string errMsg)
        {
            try
            {
                errMsg = "";
                int i = 0;
                int result = 0;
                SqlParameter[] prms = new SqlParameter[6];
                prms[i] = new SqlParameter("@mahoadon", vchitiethoadon.Mahoadon);
                i = i + 1;
                prms[i] = new SqlParameter("@masanpham", vchitiethoadon.Masanpham);
                i = i + 1;
                prms[i] = new SqlParameter("@soluong", vchitiethoadon.Soluong);
                i = i + 1;
                prms[i] = new SqlParameter("@dongia", vchitiethoadon.Dongia);
                i = i + 1;
                prms[i] = new SqlParameter("@outreturn", SqlDbType.Int, 1);
                prms[i].Direction = ParameterDirection.Output;

                result = SQLExecute.ExcuteSPOutput("dbo.sp_save_chitiet_hoadon", ref prms, 5, ref errMsg, "@outreturn");

                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return 0;
            }
        }

        public DataTable getall_sanpham(ref string errMsg)
        {
            try
            {
                errMsg = "";
                DataTable tbl = SQLExecute.LoadDataFromDB2("dbo.sp_getall_sanpham", "tmpData", ref errMsg);
                return tbl;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return null;
            }
        }

        public int Login_khachhang(string _username, string _password, ref string errMsg)
        {
            try
            {
                errMsg = "";
                int i = 0;
                int result = 0;
                SqlParameter[] prms = new SqlParameter[4];
                prms[i] = new SqlParameter("@username", _username);
                i = i + 1;
                prms[i] = new SqlParameter("@password", _password);
                i = i + 1;
                prms[i] = new SqlParameter("@outreturn", SqlDbType.Int, 1);
                prms[i].Direction = ParameterDirection.Output;

                result = SQLExecute.ExcuteSPOutput("dbo.sp_login_khachhang", ref prms, 3, ref errMsg, "@outreturn");

                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return 0;
            }
        }

        public int save_khachhang(khachhang vkhachhang, ref string errMsg)
        {
            try
            {
                errMsg = "";
                int i = 0;
                int result = 0;
                SqlParameter[] prms = new SqlParameter[10];
                prms[i] = new SqlParameter("@khachhangid", vkhachhang.Khachhangid);
                i = i + 1;
                prms[i] = new SqlParameter("@tendangnhap", vkhachhang.Tendangnhap);
                i = i + 1;
                prms[i] = new SqlParameter("@matkhau", vkhachhang.Matkhau);
                i = i + 1;
                prms[i] = new SqlParameter("@hoten", vkhachhang.Hoten);
                i = i + 1;
                prms[i] = new SqlParameter("@diachi", vkhachhang.Diachi);
                i = i + 1;
                prms[i] = new SqlParameter("@sodienthoai", vkhachhang.Sodienthoai);
                i = i + 1;
                prms[i] = new SqlParameter("@emai", vkhachhang.Email);
                i = i + 1;
                prms[i] = new SqlParameter("@trangthai", vkhachhang.Trangthai);
                i = i + 1;
                prms[i] = new SqlParameter("@outreturn", SqlDbType.Int, 1);
                prms[i].Direction = ParameterDirection.Output;

                result = SQLExecute.ExcuteSPOutput("dbo.sp_save_khachhang", ref prms, 9, ref errMsg, "@outreturn");

                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return 0;
            }
        }
    }
}